package com.ssb.plugins;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.service.impl.LoadCacheFactory;
import com.apache.cache.util.Validator;
import com.apache.database.datasource.ConnctionData;
import com.apache.database.datasource.DynamicDataSource;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.rpc.entity.RpcDatasource;
import com.apache.tools.DataMap;
import com.apache.tools.StrUtil;
import com.ssb.service.check.CheckedFrontSysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 订单详细
 * @author: Hou Dayu
 * @date: Created in 2020/2/14
 */
@Component("orderDetails")
public class OrderDetailsPluginImpl extends HheSuperPlugin implements CustomMethodPlugin {

    @Override
    public Object beforeInvoke(Map<String, Object> map) {

        String requestNo = String.valueOf(map.get("requestNo"));//投保单号
        String policyNo = String.valueOf(map.get("policyNo"));//保单号
        if(StrUtil.isNull(requestNo) && StrUtil.isNull(policyNo)){
            throw new BusinessException("投保单号或保单号不能为空");
        }
//        Map orderMap = new HashMap();
//        if(StrUtil.isNotNull(requestNo))
//        orderMap.put("ssInsurOrder.w_requestNo",requestNo);
//        if(StrUtil.isNotNull(policyNo))
//        orderMap.put("ssInsurOrder.w_policyNo",policyNo);
        String datasource = StrUtil.doNull(String.valueOf(map.get("datasource")),DATA_SOURCE);
//        orderMap.put("datasource",datasource);
//        ResultEntity entity = IusPluginUtil.getInstance().select("list","s_ssInsurOrder",orderMap);
        String sql = "select * from ss_insur_order where ";
        if(StrUtil.isNotNull(requestNo)) {
            sql += " request_no='" + requestNo + "'";
        }else {
            sql += " policy_no='" + policyNo + "'";
        }
        List list = select(sql,datasource);
//        List list = (List)entity.getEntity();
        if(Validator.isEmpty(list)){
           throw new BusinessException("保单不存在,请确认查询信息！");
        }
        DataMap order = (DataMap)list.get(0);
        Map insured = new HashMap();
        insured.put("ssInsurPerson.w_requestNo", order.get("requestNo"));
        insured.put("datasource",datasource);
        ResultEntity rent = IusPluginUtil.getInstance().select(datasource,"list","s_ssInsurPerson",insured);
        List insureds = (List)rent.getEntity();
        order.put("ssInsurPerson",insureds);
        map.put("resultData",order);
        map.put(CustomMethodPlugin.pluginInnerReturn,order);
        map.put(CustomMethodPlugin.resultMark, "T");
        return null;
    }

    @Override
    public Object doInvoke(Map<String, Object> map) {
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> map) {
        return null;
    }
}
